class ForumController < ApplicationController
  caches_page :index,:method_missing
  before_filter :user_authorize, :only =>[:create_topic,:update_topic,:delete_topic,:create_reply,:update_reply,:delete_reply]
  verify :method => :post,:only =>[:create_topic,:update_topic,:delete_topic,:create_reply,:update_reply,:delete_reply], :redirect_to => {:controller=>"info",:action => "info"}
  
  def index
    @forums=Forum.find(:all)
  end
  
  def read_forum
    id = params[:id] || -1
    @forum=Forum.find_by_id(id)
    @pages, @topics = paginate :Topics,:conditions=>["forum_id=?",@forum.id],:order=>"updated_at DESC",:per_page => 20
    render :template=>"forum/forum"
  rescue
    render :template=>"index/null"
  end
  
  def method_missing(name, *args) 
    render :template=>"index/null"
  end
  
  def create_topic
    id=params[:forum_id]|| -1
    @forum=Forum.find_by_id(id)
    @error=[]
    @filename=params[:filename]
    if @activeuser.guest?
      @error+=["<li>请登陆后再发表您的大作，谢谢！</li>"]
    elsif params[:validation]==nil || Validation.validate(session[:code],params[:validation])== false
      @error+=["<li>请完成边上的算术题，谢谢!</li>"]
    else
      @error+=Topic.validate_params(params[:topic])
    end
    session[:code]=nil 
    unless @error.length==0
      @height=@error.length*15+40
      @filename=@filename.succ
      headers["Content-Type"] = "text/javascript; charset=UTF-8"
      render :template=>"/login/check_form.rjs"
    else
      unless topic=@activeuser.create_topic(params[:topic],@forum)
        @error+=["<li>您还没有解封，无权发表信息。</li>"]
        @height=@error.length*15+40
        @filename=@filename.succ
        headers["Content-Type"] = "text/javascript; charset=UTF-8"
        render :template=>"/login/check_form.rjs"
      else
        render :update do |page|
          page.redirect_to :controller=>'forum',:action => 'read_forum',:id=>@forum.id
        end
      end
    end
  rescue
    render :template=>"index/null"
  end
  
  def read_topic
    id = params[:id] || -1
    @topic=Topic.find(id)
    @activeuser.update_correlation(@topic)
    @forum=Forum.find_by_id(@topic.forum_id)
    @topic.view_times +=  1
    @topic.save
    render :template=>"forum/topic"
  end
  
  def modify_topic
    id = params[:id] || -1
    unless @topic=Topic.find_by_id_and_user_id(id,@activeuser.user_id)
      render :inline=>%{<script type="text/javascript">openAlert('对不起，你没有权限修改该话题。',200,50);</script>}
    else
      render :partial => "modify_topic"
    end
  end
  
  def update_topic
    id = params[:id] || -1
    @topic=Topic.find_by_id_and_user_id(id,@activeuser.user_id)
    if Topic.validate_params(params[:topic]).length==0
      @topic=@activeuser.update_topic(params[:topic],@topic)
    end
    render :partial => "topic"
  end
  
  def hide_topic
    render(:nothing => true)
  end
  
  def delete_topic
    id = params[:id] || -1
    if topic=Topic.find_by_id_and_user_id(id,@activeuser.user_id)
      forum_id=topic.forum_id
      topic.destroy
    end
    redirect_to :controller=>"forum",:action=>"read_forum",:id=>forum_id
  end
  
  def create_reply
    id = params[:id] || -1
    @topic=Topic.find_by_id(id)
    @error=[]
    @filename=params[:filename]
    if @activeuser.guest?
      @error+=["<li>请登陆后再发表您的大作，谢谢！</li>"]
    elsif params[:validation]==nil || Validation.validate(session[:code],params[:validation])== false
      @error+=["<li>请完成边上的算术题，谢谢!</li>"]
    else
      @error+=Comment.validate_params(params[:reply])
    end
    session[:code]=nil 
    unless @error.length==0
      @height=@error.length*15+40
      @filename=@filename.succ
      headers["Content-Type"] = "text/javascript; charset=UTF-8"
      render :template=>"/login/check_form.rjs"
    else
      unless reply=@activeuser.create_comment(params[:reply],@topic)
        @error+=["<li>您还没有解封，无权发表信息。</li>"]
        @height=@error.length*15+40
        @filename=@filename.succ
        headers["Content-Type"] = "text/javascript; charset=UTF-8"
        render :template=>"/login/check_form.rjs"
      else
        @topic.forum.update_forum(@activeuser.user)
        render :update do |page|
          page.insert_html(:bottom, 'reply_list',  :partial => 'reply', :object => reply)
        end
      end
    end
  end
  
  def modify_reply
    id = params[:id] || -1
    unless @reply=Comment.find_by_id_and_user_id(id,@activeuser.user_id)
      render :inline=>%{<script type="text/javascript">openAlert('对不起，你没有权限修改该回复。',200,50);</script>}
    else
      render :partial => "modify_reply"
    end
  end
  
  def update_reply
    id = params[:id] || -1
    @reply=Comment.find_by_id_and_user_id(id,@activeuser.user_id)  
    if Comment.validate_params(params[:reply]).length==0
      @reply=@activeuser.update_comment(params[:reply],@reply)
    end
    render :partial => "reply", :locals =>{:reply=>@reply} if request.post?
  end
  
  def hide_reply
    render(:nothing => true)
  end
  
  def delete_reply
    id = params[:id] || -1
    if reply=Comment.find_by_id_and_user_id(id,@activeuser.user_id)
      reply.destroy
      render :inline=>"<script type='text/javascript'>new Effect.Puff('reply"+id+"');</script>"
    else
      render :inline=>%{<script type="text/javascript">openAlert('对不起，你没有权限修改该回复。',200,50);</script>}
    end
  end
end
